'use strict';

import verification from './VerificationUtil';
import cache        from './CacheUtil';
import baseParam    from './BaseParam';
import messageUtil  from './MessageUtil';
import httpUtil     from './HttpUtil';
import dateUtil     from './DateUtil';

/**
 * 工具类
 * @type {{getCacheUtil(): {set, get, storage}, formatDate(*=, *=, *=, *=): string, getInfo(): {message: info.message, error: info.error}, getBaseData(): {SERIAL_NUM, VERSION, ACTION_ADDRESS, DATE_TEXT, WEB_SOCKET_ADDRESS}, getVerification(): {isNull: (function(*=): boolean), isNotNull: (function(*=): boolean), isArray: (function(*): boolean), isNotArray: (function(*=): boolean), isNumber: (function(*): boolean), isNotNumber: (function(*=): boolean)}, getQueryByName(*): *, storage(*=, *=): *, getHttpUtil(): {ajaxGet, ajaxPost, ajax}, removeArray: (function(*, *): Array), ajaxPost(*=, *=, *=, *=): void, ajax(*=, *=, *=, *=, *=): void, getDateUtil(): {formatDate}, ajaxGet(*=, *=, *=, *=): void, between(*=, *=, *=): *}}
 */
const baseUtil = {
    /**
     * 获取初始参数信息
     * @return {{SERIAL_NUM, VERSION, ACTION_ADDRESS, DATE_TEXT, WEB_SOCKET_ADDRESS}}
     */
    getBaseData() {
        let baseData = baseParam;
        // 加入http参数
        if(httpUtil && httpUtil.getParam()) {
            let httpParam = httpUtil.getParam();
            Object.assign(baseData, httpParam);
        }
        return baseData;
    },
    /**
     * 获取验证数据方法
     * @returns {{isNull: (function(*=): boolean), isNotNull: (function(*=): boolean), isArray: (function(*): boolean), isNotArray: (function(*=): boolean), isNumber: (function(*): boolean), isNotNumber: (function(*=): boolean)}}
     */
    getVerification() {
        return verification;
    },
    /**
     * 获取信息提示
     * @returns {{message: info.message, error: info.error}}
     */
    getInfo() {
        return messageUtil;
    },
    /**
     * 获取缓存工具
     * @returns {{set, get, storage}}
     */
    getCacheUtil(){
        return cache;
    },
    /**
     * 获取http工具
     * @returns {{ajaxGet, ajaxPost, ajax}}
     */
    getHttpUtil() {
        return httpUtil;
    },
    /**
     * 获取时间工具
     * @returns {{formatDate}}
     */
    getDateUtil() {
        return dateUtil;
    },
    /**
     * ajaxPost请求
     * @param url
     * @param params
     * @param success
     * @param type
     */
    ajaxPost(url, params, success, type = baseData.RequestMethod.JSON) {
        this.ajax(url, params, success, baseData.RequestMethod.POST, type);
    },
    /**
     * ajaxGet请求
     * @param url
     * @param params
     * @param success
     * @param type
     */
    ajaxGet(url, params, success, type = baseData.RequestMethod.JSON) {
        this.ajax(url, params, success, baseData.RequestMethod.GET, type);
    },
    /**
     * ajax请求
     * @param url
     * @param params
     * @param success
     * @param method
     * @param type
     */
    ajax(url, params, success, method, type = baseData.RequestMethod.JSON) {
        httpUtil.ajax(url, params, success, method, type);
    },
    /**
     * 格式化时间
     * @param value
     * @param format
     * @param language
     * @param getObject
     * @returns {string}
     */
    formatDate(value, format, language, getObject) {
        return dateUtil.formatDate(value, format, language, getObject);
    },
    /**
     * 获取url参数
     * @param param
     * @returns {*}
     */
    getQueryByName(param) {
        let reg = new RegExp("(^|&)" + param + "=([^&]*)(&|$)");
        let r = window.location.search.substr(1).match(reg);
        if (r != null)
            return decodeURI(r[2]);
        return null;
    },
    /**
     * 数组中移除某个值
     * @param array
     * @param value
     * @returns {Array}
     */
    removeArray: function(array, value) {
        let resultArray = [];
        for(let i = 0; i < array.length; i++) {
            if(array[i] !== value) {
                resultArray.push(array[i]);
            }
        }
        return resultArray;
    },
    /**
     * 验证数字是否在两数之间
     * @param value
     * @param number1
     * @param number2
     * @returns {*}
     */
    between(value, number1, number2) {
        if(verification.isNotNumber(value) || verification.isNotNumber(number1) || verification.isNotNumber(number2)) {
            return null;
        }
        return value > number1 && value < number2;
    },
    /**
     * 调用浏览器缓存
     * @param name
     * @param value
     * @returns {*}
     */
    storage(name, value) {
        return cache.storage(name, value);
    }
};

const baseData = baseUtil.getBaseData();

// 暴露信息
export{
    baseUtil,
}

